<?php 
/**
 * @version        $Id$
 * @author         master@xuewl.com
 * @copyright      Copyright (c) 2007 - 2014, Chongqing xuewl Information Technology Co., Ltd.
 * @link           http://www.xuewl.com
**/
namespace Member\Controller;
use Member\Controller\InitController;
class SignController extends InitController {
	public function _initialize() {
		parent::_initialize();
		$this->db = D('MemberSign');
	}

	public function index() {
		$SEO = seo(0, '史上最牛签到');
		include template('sign_index');
	}

	/* 签到按钮 */
	public function checkBegin() {
		$sqlmap = array();
		$sqlmap['uid'] = $this->userid;
		$sqlmap['_string'] = "DATE_FORMAT(FROM_UNIXTIME(dateline),'%Y%m%d') = DATE_FORMAT(NOW(),'%Y%m%d')";
		if($this->db->where($sqlmap)->count()) {
			$this->error('您今日已签到');
		} else {
			$sysauth = authcode($this->userid.'|'.NOW_TIME, 'ENCODE');
			$message = '<a href="javascript:void(0);" class="qdon" onclick="checkIn(\''.$sysauth.'\')">立即签到</a>';
			$this->success($message);
		}
	}

	/* 签到 */
	public function checkin() {
		$uid = $this->userid;
		$sqlmap = array();
		$sqlmap['uid'] = $uid;
		$sqlmap['_string'] = "DATE_FORMAT(FROM_UNIXTIME(dateline),'%Y%m%d') = DATE_FORMAT(NOW(),'%Y%m%d')";
		$count = $this->db->where($sqlmap)->count();
		if($count) {
			$this->error('您今日已签到');
		} else {
			$result = array();
			$result['status'] = 1;
			
			$result['insertG'] = round(($this->userinfo['u_coin'] * 3) / 10000);//应得奖励
			$result['userG'] = $this->userinfo['u_coin'] + $result['insertG'];//目前U币总数
			$result['treasure'] = $count + 1;//签到次数
			if ($result['insertG'] > 0) {
				M('Member')->where(array('userid' => $uid))->setInc("u_coin", $result['insertG']);
				$message = '用户签到奖励';
				score_change($uid, $message, $result['insertG'], 'u_coin', "1");
				$sign_info = array('uid' => $uid, 'u_coin' => $result['insertG']);
				$this->db->update($sign_info);
			}
		}
		echo json_encode($result);
	}
}